package com.alks.function.service.impl.yield;

import com.alibaba.fastjson2.JSON;
import com.alks.common.service.RedisService;
import com.alks.common.utils.excelUtils.ExcelUtils;
import com.alks.entity.data.entity.CutYield;
import com.alks.entity.data.entity.OutStockDetail;
import com.alks.entity.data.enums.ResultCodeEnum;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.dto.CutYieldDto;
import com.alks.function.data.dto.OutStockDetailDto;
import com.alks.function.data.request.yield.CutYieldRequest;
import com.alks.function.mapper.PcYpLotHeadMapping;
import com.alks.function.service.CutYieldService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author:xxxxx
 * @create: 2023-07-12 10:00
 * @Description: 裁断产量
 */
@Service
public class CutYieldServiceImpl implements CutYieldService {
    @Autowired
    private PcYpLotHeadMapping pcYpLotHeadMapping;
    @Autowired
    private RedisService redisService;
    @Override
    public ResponseInfo cutYieldList(CutYieldRequest request) {
        if (request == null){
            return ResponseInfo.error(ResultCodeEnum.LOGIN_CONFIG_IS_NULL.getMessage(), ResultCodeEnum.LOGIN_CONFIG_IS_NULL.getCode());
        }
        if (request.getPageNum() == null || request.getPageSize() == null) {
            request.setPageNum(1);
            request.setPageSize(10);
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize(),false);
        CutYieldDto cutYieldDto = new CutYieldDto();
        List<CutYield> list = pcYpLotHeadMapping.cutYieldList(request);
        List<Map<String, BigDecimal>> totals = pcYpLotHeadMapping.getOrderAndProdTotal(request);
        Long total = pcYpLotHeadMapping.getTotal(request);
        cutYieldDto.setList(list);
        AtomicReference<BigDecimal> orders = new AtomicReference<>();
        AtomicReference<BigDecimal> prods = new AtomicReference<>();
        totals.forEach(x-> {
            if (x == null){
                return;
            }
            if (x.get("prods") != null) {
                prods.set(x.get("prods"));
            }
            if (x.get("orders") !=null){
                orders.set(x.get("orders"));
            }
        });
        cutYieldDto.setOrderTotal(orders.get());
        cutYieldDto.setProTotal(prods.get());
        cutYieldDto.setTotal(total);
        return ResponseInfo.ok(cutYieldDto);
    }

    @Override
    public ResponseInfo getCutYieldCondition() {
        Set set = redisService.scan("cutyield");
        if (set.size()>0){
            String cutyield = redisService.get("cutyield");
            HashMap<String,List<String>> map = JSON.parseObject(cutyield, HashMap.class);
            return ResponseInfo.ok(map);
        }
        HashMap<String, List<String>> map = new HashMap<>();
        List<String> duty = pcYpLotHeadMapping.getDutyDept();
        List<String> works = pcYpLotHeadMapping.getWorker();
        List<String> brand = pcYpLotHeadMapping.getBrand();
        List<String> types = new ArrayList<>(Arrays.asList("正单","补单","试板","大货","全部"));
        map.put("duty",duty);
        map.put("brands",brand);
        map.put("works",works);
        map.put("types",types);
        String s = JSON.toJSONString(map);
        redisService.set("cutyield",s);
        return ResponseInfo.ok(map);
    }

    @Override
    public ResponseInfo stitchYieldList(CutYieldRequest request) {
        if (request == null){
            return ResponseInfo.error(ResultCodeEnum.LOGIN_CONFIG_IS_NULL.getMessage(), ResultCodeEnum.LOGIN_CONFIG_IS_NULL.getCode());
        }
        if (request.getPageNum() == null || request.getPageSize() == null) {
            request.setPageNum(1);
            request.setPageSize(10);
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize(),false);
        CutYieldDto cutYieldDto = new CutYieldDto();
        List<CutYield> list = pcYpLotHeadMapping.stitchYieldList(request);
        List<Map<String, BigDecimal>> totals = pcYpLotHeadMapping.getStitchOrderAndProdTotal(request);
        Long total = pcYpLotHeadMapping.getStitchTotal(request);
        cutYieldDto.setList(list);
        AtomicReference<BigDecimal> orders = new AtomicReference<>();
        AtomicReference<BigDecimal> prods = new AtomicReference<>();
        totals.forEach(x-> {
            if (x == null){
                return;
            }
            if (x.get("prods") != null) {
                prods.set(x.get("prods"));
            }
            if (x.get("orders") !=null){
                orders.set(x.get("orders"));
            }
        });
        cutYieldDto.setOrderTotal(orders.get());
        cutYieldDto.setProTotal(prods.get());
        cutYieldDto.setTotal(total);
        return ResponseInfo.ok(cutYieldDto);
    }

    @Override
    public ResponseInfo outStockDetail(CutYieldRequest request) {
        if (request == null){
            return ResponseInfo.error(ResultCodeEnum.LOGIN_CONFIG_IS_NULL.getMessage(), ResultCodeEnum.LOGIN_CONFIG_IS_NULL.getCode());
        }
        if (request.getPageNum() == null || request.getPageSize() == null) {
            request.setPageNum(1);
            request.setPageSize(10);
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize(),false);
        OutStockDetailDto dto = new OutStockDetailDto();
        List<OutStockDetail> list = pcYpLotHeadMapping.getOutStockDetail(request);
        Long total = pcYpLotHeadMapping.getOutStockTotal(request);
        Long outTotal = pcYpLotHeadMapping .getOutTotal(request);
        dto.setList(list);
        dto.setTotalOuts(outTotal);
        dto.setTotal(total);
        return ResponseInfo.ok(dto);
    }

    @Override
    public void downloadCutYield(HttpServletResponse response, CutYieldRequest request,String choose) throws IOException {
        List<CutYield> list = null;
        Map<String, Object> map = new HashMap<>();
        if ("1".equals(choose)){
             list = pcYpLotHeadMapping.cutYieldList(request);
             map.put("name","裁断");
        }else if ("2".equals(choose)){
            list = pcYpLotHeadMapping.stitchYieldList(request);
            map.put("name","针车");
        }
        BigDecimal total = new BigDecimal(0);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date = format.format(new Date());
        assert list != null;
        for (CutYield cutYield : list) {
            if (cutYield.getProdQty()!=null){
                total = total.add(cutYield.getProdQty());
            }
        }
        map.put("total",total);
        map.put("date",date);
        ExcelUtils.creatByTemplate(response,"研发裁断完工日报表模板.xls",date,list,map);
    }
}
